import { useLocation } from "react-router-dom";
import DBreadcrumb from "knowdesign/lib/extend/d-breadcrumb";

const routes = [
  {
    path: "/user",
    layout: false,
    routes: [
      {
        name: "login",
        path: "/user/login",
        component: "./Login",
      },
      {
        component: "./404",
      },
    ],
  },
  {
    path: "/data-collect",
    name: "数据采集",
    icon: " ",
    routes: [
      {
        path: "/data-collect",
        redirect: "/data-collect/job",
      },
      {
        icon: "product",
        name: "同步任务",
        path: "/data-collect/job",
        component: "./data-collect/job",
      },
      {
        icon: "hdd",
        name: "数据源",
        path: "/data-collect/data-source",
        component: "./data-collect/data-source",
      },
      {
        icon: "smile",
        name: "任务运维",
        path: "/data-collect/summary",
        component: "./data-collect/summary",
      },
      {
        icon: "smile",
        name: "执行器",
        path: "/data-collect/executor",
        component: "./data-collect/executor",
      },
    ],
  },
  {
    path: "/resource-center",
    name: "资源中心",
    icon: " ",
    routes: [
      {
        path: "/resource-center",
        redirect: "/resource-center/file",
      },
      {
        icon: "file",
        name: "文件管理",
        path: "/resource-center/file",
        component: "./resource-center/file",
      },
    ],
  },
  {
    path: "/dw-plan",
    name: "数仓规划",
    icon: " ",
    routes: [
      {
        path: "/dw-plan",
        redirect: "/dw-plan/business-process",
      },
      {
        icon: "product",
        name: "业务过程",
        path: "/dw-plan/business-process",
        component: "./dw-plan/business-process",
      },
      {
        icon: "hdd",
        name: "数仓分层",
        path: "/dw-plan/dw-layer",
        component: "./dw-plan/dw-layer",
      },
      {
        icon: "partition",
        name: "业务分类",
        path: "/dw-plan/dw-classification",
        routes: [
          {
            path: "/dw-plan/dw-classification",
            redirect: "/dw-plan/dw-classification/A",
          },
          {
            icon: " ",
            name: "业务分类1",
            path: "/dw-plan/dw-classification/A",
            component: "./dw-plan/dw-classification/A",
          },
          {
            icon: " ",
            name: "业务分类2",
            path: "/dw-plan/dw-classification/B",
            component: "./dw-plan/dw-classification/B",
          },
        ],
      },
    ],
  },
  {
    path: "/data-center",
    name: "数据中心",
    icon: " ",
    routes: [
      {
        path: "/data-center",
        redirect: "/data-center/home",
      },
      {
        icon: "smile",
        name: "首页",
        path: "/data-center/home",
        component: "./data-modal",
      },
    ],
  },

  {
    path: "/config",
    name: "",
    icon: " ",
    routes: [
      {
        path: "/config",
        redirect: "/config/setting",
      },
      {
        icon: "smile",
        name: "配置管理",
        path: "/config/setting",
        component: "./config/setting",
      },
      {
        icon: "user",
        name: "用户管理",
        path: "/config/user",
        component: "./config/user",
      },
      {
        icon: "file",
        name: "审计日志",
        path: "/config/oplog",
        component: "./config/oplog",
      },
    ],
  },

  {
    path: "/",
    redirect: "/dw-plan",
  },
  {
    component: "./404",
  },
];
const generateBreadcrumbs = (routes: any, pathname: any) => {
  const breadcrumbs: any[] = [];

  const findBreadcrumbs = (routes: any[], path: any) => {
    for (const route of routes) {
      if (route.path && path === route.path) {
        if (route.name) {
          breadcrumbs.push({ label: route.name, aHref: route.path });
        }
        return true;
      }
      if (route.routes) {
        for (const subRoute of route.routes) {
          // Use a RegExp to check if the path starts with the subRoute.path
          if (path.startsWith(subRoute.path)) {
            if (findBreadcrumbs(route.routes, path)) {
              if (route.name) {
                breadcrumbs.unshift({ label: route.name, aHref: route.path });
              }
              return true;
            }
          }
        }
      }
    }
    return false;
  };

  findBreadcrumbs(routes, pathname);

  return breadcrumbs;
};
const Breadcrumbs = () => {
  const location = useLocation();
  const pathname = location.pathname;
  const breadcrumbs = generateBreadcrumbs(routes, pathname);
  // Set the aHref of the last breadcrumb to an empty string
  if (breadcrumbs.length > 0) {
    breadcrumbs[breadcrumbs.length - 1].aHref = "";
  }

  return <DBreadcrumb breadcrumbs={breadcrumbs} />;
};

export default Breadcrumbs;
